using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameStateManagement
{

    public class Planet : Actor
    {
        // The softness of the planet (used for ship collisions)
        public static float Bounciness = 0.5f;

        private int starSize = 1;
        public Vector3 directionVector = Vector3.Zero;
        Texture2D texture;
        Texture2D normal_map;

        public Planet(Game game)
            : base(game)
        {
            m_fScale = 1f;
            sMeshName = "Planet";
            fMass = 0.1f;
            fTerminalVelocity = 100.0f;
            vAcceleration = Vector3.Zero;
            bPhysicsDriven = false;

            texture = Game.Content.Load<Texture2D>("planet_texture");
            normal_map = Game.Content.Load<Texture2D>("planet_texture_normal");

            use_effect = true;
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public void setStarSizeMultiplier(int size)
        {
            starSize = size;
        }

        public override void draw_with_effect()
        {
            effect = Game.Content.Load<Effect>("bump_map");
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            ActorModel.CopyAbsoluteBoneTransformsTo(ActorBones);
            foreach (ModelMesh mesh in ActorModel.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    part.Effect = effect;

                    world = Matrix.CreateScale(m_fScale) * Matrix.CreateTranslation(WorldTransform.Translation);

                    Matrix worldInverseTranspose = Matrix.Transpose(Matrix.Invert(mesh.ParentBone.Transform * world));

                    effect.Parameters["World"].SetValue(world * mesh.ParentBone.Transform);

                    effect.Parameters["View"].SetValue(GameplayScreen.FollowCamera.m_CameraMatrix);

                    effect.Parameters["Projection"].SetValue(GameplayScreen.FollowCamera.m_ProjectionMatrix);

                    effect.Parameters["WorldInverseTranspose"].SetValue(worldInverseTranspose);

                    effect.Parameters["ViewVector"].SetValue(GameplayScreen.FollowCamera.WorldTransform.Forward);

                    effect.Parameters["ModelTexture"].SetValue(texture);
                    
                    effect.Parameters["NormalMap"].SetValue(normal_map);

                    effect.Parameters["scale"].SetValue(m_fScale);

                    Vector3 direction = new Vector3();

                    direction = m_vWorldPosition - GameplayScreen.FollowCamera.m_vWorldPosition;
                    direction.Normalize();
                    effect.Parameters["DiffuseLightDirection"].SetValue(direction);
                }
                mesh.Draw();
            }
        }
    }
}
